package com.cloudpark.common.util.wechat;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.cloudpark.common.util.ReadProperties;

public class SignUtil {

    // 与开发模式接口配置信息中的Token保持一致
    private static List<String> tokens;

    static {
        // 所有公用云平台的微信公众号token集合
        tokens = new ArrayList<String>();
        String allTokens = ReadProperties.getProperties("wechat_all_tokens");
        String[] strings = allTokens.split(",");
        for (int i = 0; i < strings.length; i++) {
            tokens.add(strings[i]);
        }
    }

    /**
     * 校验签名
     * 
     * @param signature
     *            微信加密签名
     * @param timestamp
     *            时间戳
     * @param nonce
     *            随机数
     * @return
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        boolean success = false;
        for (String token : tokens) {
            // 对token、timestamp和nonce按字典排序
            String[] paramArr = new String[] { token, timestamp, nonce };
            Arrays.sort(paramArr);

            // 将排序后的结果拼接成一个字符串
            String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);

            String ciphertext = null;
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                // 对接后的字符串进行sha1加密
                byte[] digest = md.digest(content.toString().getBytes());
                ciphertext = byteToStr(digest);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            // 将sha1加密后的字符串与signature进行对比
            success = ciphertext != null ? ciphertext.equals(signature.toUpperCase()) : false;
            if (success) {
                break;
            }
        }
        return success;
    }

    /**
     * 将字节数组转换为十六进制字符串
     * 
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     * 
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }
}
